﻿using System;
using System.Collections.Generic;
using System.Linq;

public static class ExpandClass
{
    /// <summary>
    /// 删除指定元素，且后面的元素往前补
    /// </summary>
    /// <param name="array"></param>
    /// <param name="idx"></param>
    /// <param name="lastValue">后面元素补</param>
    public static void ERemoveAt<T>(this T[] array, int idx, T lastValue) where T : struct
    {
        int length = array.Length;
        for (int i = idx; i < length; i++)
        {
            if (i == length - 1)
            {
                array[i] = lastValue;
            }
            else
            {
                array[i] = array[i + 1];
            }
        }
    }

    public static void CLearAllEvent(Action mEvent)
    {
        var delegates = mEvent.GetInvocationList();
        foreach (var del in delegates)
        {
            mEvent -= del as Action;
        }
    }

    public static void CLearAllEvent(Action<object> mEvent)
    {
        var delegates = mEvent.GetInvocationList();
        foreach (var del in delegates)
        {
            mEvent -= del as Action<object>;
        }
    }

    public static void CLearAllEvent(Action<object, object> mEvent)
    {
        var delegates = mEvent.GetInvocationList();
        foreach (var del in delegates)
        {
            mEvent -= del as Action<object, object>;
        }
    }

    public static T[] CreateArray<T>(int num) where T : new()
    {
        var result = new T[num];
        for (var i = 0; i < num; i++)
        {
            result[i] = new T();
        }
        return result;
    }

    public static List<T> CreateList<T>(int num) where T : new()
    {
        var result = new List<T>(num);
        for (var i = 0; i < num; i++)
        {
            result[i] = new T();
        }
        return result;
    }

    public static void ForEach<T>(this T[] array, Action<T> action)
    {
        if (array != null && action != null)
        {
            Array.ForEach(array, action);
        }
    }

    public static List<KeyValuePair<TKey, TValue>> ToKeyValueList<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
    {
        var keyValues = new List<KeyValuePair<TKey, TValue>>();
        if (dictionary != null)
        {
            foreach (var item in dictionary)
            {
                var keyValue = new KeyValuePair<TKey, TValue>(item.Key, item.Value);
                keyValues.Add(keyValue);
            }
        }
        return keyValues;
    }

    public static KeyValuePair<TKey, TValue>[] ToKeyValueArray<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
    {
        int idx = 0; var keyValues = new KeyValuePair<TKey, TValue>[idx];
        if (dictionary != null)
        {
            keyValues = new KeyValuePair<TKey, TValue>[dictionary.Count];
            foreach (var item in dictionary)
            {
                var keyValue = new KeyValuePair<TKey, TValue>(item.Key, item.Value);
                keyValues[idx] = keyValue; idx = idx + 1;
            }
        }
        return keyValues;
    }

    public static TKey GetKeyByIdx<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, int idx, TKey defalt)
    {
        if (dictionary == null) { return defalt; }
        TKey[] keys = Enumerable.ToArray(dictionary.Keys);

        if (idx >= 0 && idx < keys.Length)
        {
            return keys[idx];
        }
        return defalt;
    }

    public static TValue GetValueByIdx<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, int idx, TValue defalt)
    {
        if (dictionary == null) { return defalt; }
        TValue[] values = Enumerable.ToArray(dictionary.Values);

        if (idx >= 0 && idx < values.Length)
        {
            return values[idx];
        }
        return defalt;
    }

    public static TValue GetValueByKey<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue defalt)
    {
        if (dictionary == null) { return defalt; }
        if (dictionary.ContainsKey(key))
        {
            return dictionary[key];
        }
        return defalt;
    }

    public static T GetValueByIdx<T>(this T[] array, int idx, T defalt)
    {
        if (array == null) { return defalt; }
        if (idx >= 0 && idx < array.Length)
        {
            return array[idx];
        }
        return defalt;
    }

    public static T GetValueByIdx<T>(this List<T> list, int idx, T defalt)
    {
        if (list == null) { return defalt; }
        if (idx >= 0 && idx < list.Count)
        {
            return list[idx];
        }
        return defalt;
    }

    public static bool ToBool(this string str)
    {
        bool temp = false;
        if (!string.IsNullOrEmpty(str))
        {
            bool.TryParse(str, out temp);
        }
        return temp;
    }

    public static int ToInt(this string str)
    {
        int temp = 0;
        if (!string.IsNullOrEmpty(str))
        {
            int.TryParse(str, out temp);
        }
        return temp;
    }

    public static float ToFloat(this string str)
    {
        float temp = 0;
        if (!string.IsNullOrEmpty(str))
        {
            float.TryParse(str, out temp);
        }
        return temp;
    }

    public static long ToLong(this string str)
    {
        long temp = 0;
        if (!string.IsNullOrEmpty(str))
        {
            long.TryParse(str, out temp);
        }
        return temp;
    }

    public static List<int> ToIntList(this string str, string sep = ",")
    {
        List<int> list = new List<int>(); int id = 0;
        if (string.IsNullOrEmpty(str)) { return list; }

        string[] strClip = str.Split(sep.ToCharArray());
        for (int idx = 0; idx < strClip.Length; idx++)
        {
            int.TryParse(strClip[idx], out id);
            if (id > 0) { list.Add(id); }
        }
        return list;
    }

    public static List<float> ToFloatList(this string str, string sep)
    {
        List<float> list = new List<float>(); float id = 0;
        if (string.IsNullOrEmpty(str)) { return list; }

        string[] strClip = str.Split(sep.ToCharArray());
        for (int idx = 0; idx < strClip.Length; idx++)
        {
            float.TryParse(strClip[idx], out id);
            if (id > 0) { list.Add(id); }
        }
        return list;
    }
}